package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import xyz.hubery.domain.Coin;
import xyz.hubery.dto.CoinDTO;
import xyz.hubery.feign.CoinServiceFeign;
import xyz.hubery.mappers.CoinMappers;
import xyz.hubery.model.R;
import xyz.hubery.service.CoinService;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/coins")
@Api("币种管理接口")
public class CoinsController implements CoinServiceFeign {

    @Autowired
    private CoinService coinService;

    @GetMapping
    @ApiOperation(value = "分页条件查询数字货币")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,value = "数字货币的名称") ,
            @ApiImplicitParam(name = "type" ,value = "数字货币类型的名称") ,
            @ApiImplicitParam(name = "status" ,value = "数字货币类型的状态") ,
            @ApiImplicitParam(name = "status" ,value = "数字货币类型的标题") ,
            @ApiImplicitParam(name = "wallet_type" ,value = "数字货币钱包类型") ,
            @ApiImplicitParam(name = "current" ,value = "当前页") ,
            @ApiImplicitParam(name = "size" ,value = "每页显示的条数") ,
    })
    public R<Page<Coin>> findByPage(
            Coin coin,@RequestParam(name = "wallet_type",required = false) String walletType ,
            @ApiIgnore Page<Coin> page
    ){
        page.addOrder(OrderItem.desc("last_update_time"));
        coin.setWallet(walletType);
        Page<Coin> coinPage = coinService.findByPage(coin,page) ;
        return R.ok(coinPage) ;
    }


    @GetMapping("/info/{id}")
    @ApiOperation("查询货币信息")
    public R<Coin> findById(@PathVariable("id") Long id){
        Coin coin = coinService.getById(id) ;
        return R.ok(coin) ;
    }

    @PatchMapping
    @ApiOperation(value = "修改我们的币种的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coin" ,value = "coin 的 json 数据")
    })
    public R update(@RequestBody @Validated Coin coin){
        boolean updateById = coinService.updateById(coin);
        if(updateById){
            return R.ok() ;
        }
        return R.fail("修改失败") ;
    }

    @GetMapping("/all")
    @ApiOperation("根据状态查询所有的币种信息")
    public R<List<Coin>> findAllByStatus(@RequestParam(name = "status",required = false) Byte status){
        return R.ok(coinService.findByStatus(status)) ;
    }

    @PostMapping
    @ApiOperation(value = "新增我们的币种的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coin" ,value = "coin 的 json 数据")
    })
    public R<Coin> save(@RequestBody @Validated Coin coin){
        coin.setStatus((byte)1);
        boolean save = coinService.save(coin);
        // coin 新增成功后,会有 Id ,这是 mybatis-plus 在新增成功后,
        // 会自动的进行一个 sql 语句的查询,查询的结果就是 id,之后把 id 设置给 coin
        if(save){
            return R.ok(coin) ;
        }
        return R.fail("新增失败") ;
    }

    @GetMapping("/getCoinName")
    @Override
    public String getCoinName(@RequestParam Long coinId) {
        Coin coin = coinService.getById(coinId);
        if(coin == null || coin.getStatus() != 1){
            return null ;
        }
        return coin.getName() ;
    }

    @GetMapping("/list")
    @Override
    public List<CoinDTO> list(@RequestParam List<Long> coinIds) {
        if(CollectionUtils.isEmpty(coinIds)){
            return Collections.emptyList() ;
        }
        List<Coin> coins = coinService.listByIds(coinIds);

        return CoinMappers.INSTANCE.dtoToEntity(coins) ;
    }
}
